/**
 * Created by nndiem on 03.10.2014.
 */

appSkeleton.service('UserServiceSkeleton',['ConfigUtilSkeleton','t2iSpinnerService','$q','$http','UtilServicesSkeleton',
    function(ConfigUtilSkeleton,t2iSpinnerService,$q,$http,UtilServicesSkeleton) {
    var userAdditionalProperties = '/xexp/rest/user/additionalproperties/{projectName}/{projectId}';
    return {
        findUserAdditionalProperties: function (projectName, projectId) {
            t2iSpinnerService.start();
            var defer = $q.defer();
            var fixUrl = userAdditionalProperties.replace("{projectName}", projectName).replace("{projectId}", projectId);
            $http({url: ConfigUtilSkeleton.getBaseUrl() + fixUrl,
                dataType: 'json',
                method: 'GET',
                headers: {
                    'Content-Type': "application/vnd.ch.xpertline.xexp.data.interfaces+json"
                }
            }).then(
                function success(result) {
                    defer.resolve(result);
                    t2iSpinnerService.stop();
                    UtilServicesSkeleton.loginTimeout(result);
                },
                function error(error) {
                    defer.reject(error);
                    t2iSpinnerService.stop();
                    UtilServicesSkeleton.loginTimeout(error);
                }
            );
            return defer.promise;
        },
        saveUserAdditionalProperties: function (projectName, projectId, data) {
            var defer = $q.defer();
            var isConnected = false;
            var fixUrl = userAdditionalProperties.replace("{projectName}", projectName).replace("{projectId}", projectId);
            $.ajax({
				url: ConfigUtilSkeleton.getBaseUrl() + fixUrl,
				method: "GET",
				async: !1,
				success: function (result) {
					isConnected = true;
				},
				error: function(result, err){
					isConnected = true;
				}
            });
			
            if (!isConnected) return;
			
            $http({
                url: ConfigUtilSkeleton.getBaseUrl() + fixUrl,
                dataType: 'json',
                method: 'POST',
                data: new Blob([JSON.stringify(data)]),
                headers: {
                    'Content-Type': "application/vnd.ch.xpertline.xexp.data.interfaces+json"
                }
            }).then(
                function success(result) {
                    defer.resolve(result);
                    UtilServicesSkeleton.loginTimeout(result);
                },
                function error(error) {
                    defer.reject(error);
                    UtilServicesSkeleton.loginTimeout(error);
                }
            );
            return defer.promise;
        },
        getUserProfile: function () {
            //t2iSpinnerService.start();
            var defer = $q.defer();
            var userPermissionsUrl = '/xexp/rest/user/profile';
            $http.get(ConfigUtilSkeleton.getBaseUrl() + userPermissionsUrl).then(
                function success(result) {
                    defer.resolve(result);
                    UtilServicesSkeleton.loginTimeout(result);
                },
                function error(error) {
                    defer.reject(error);
                    UtilServicesSkeleton.loginTimeout(error);
                }
            );
            return defer.promise;
        }
    }
}]);